﻿Imports System.Text

Friend Class OdbcConnectionProperties
    Inherits AdoDotNetConnectionProperties
    ' Methods
    Public Sub New()
        MyBase.New("System.Data.Odbc")
    End Sub

    Private Shared Function ManagedSQLGetInstalledDrivers() As List(Of String)
        Dim lpszBuf As Char() = New Char(&H400 - 1) {}
        Dim pcbBufOut As Integer = 0
        Dim flag As Boolean = True
        Dim list As New List(Of String)
        Try
            flag = NativeMethods.SQLGetInstalledDrivers(lpszBuf, lpszBuf.Length, (pcbBufOut))
            Do While ((flag AndAlso (pcbBufOut > 0)) AndAlso ((pcbBufOut = (lpszBuf.Length - 1)) AndAlso (lpszBuf.Length < Math.Pow(2, 30))))
                lpszBuf = New Char((lpszBuf.Length * 2) - 1) {}
                flag = NativeMethods.SQLGetInstalledDrivers(lpszBuf, lpszBuf.Length, (pcbBufOut))
            Loop
        Catch exception1 As Exception
            flag = False
        End Try
        If flag Then
            Dim startIndex As Integer = 0
            Dim i As Integer = Array.IndexOf(Of Char)(lpszBuf, ChrW(0), startIndex, (pcbBufOut - 1))
            Do While (startIndex < (pcbBufOut - 1))
                list.Add(New String(lpszBuf, startIndex, (i - startIndex)))
                startIndex = (i + 1)
                i = Array.IndexOf(Of Char)(lpszBuf, ChrW(0), startIndex, ((pcbBufOut - 1) - i))
            Loop
        End If
        Return list
    End Function


    ' Properties
    Public Overrides ReadOnly Property IsComplete As Boolean
        Get
            If ((Not TypeOf MyBase.ConnectionStringBuilder.Item("DSN") Is String OrElse (TryCast(MyBase.ConnectionStringBuilder.Item("DSN"), String).Length = 0)) AndAlso (Not TypeOf MyBase.ConnectionStringBuilder.Item("DRIVER") Is String OrElse (TryCast(MyBase.ConnectionStringBuilder.Item("DRIVER"), String).Length = 0))) Then
                Return False
            End If
            Return True
        End Get
    End Property

    Public Shared ReadOnly Property SqlNativeClientDrivers As List(Of String)
        Get
            If (OdbcConnectionProperties._sqlNativeClientDrivers Is Nothing) Then
                OdbcConnectionProperties._sqlNativeClientDrivers = New List(Of String)
                Dim str As String
                For Each str In OdbcConnectionProperties.ManagedSQLGetInstalledDrivers
                    If (str.Contains("Native") AndAlso str.Contains("Client")) Then
                        Dim retBuffer As New StringBuilder(&H400)
                        If ((NativeMethods.SQLGetPrivateProfileString(str, "Driver", "", retBuffer, retBuffer.Capacity, "ODBCINST.INI") > 0) AndAlso (retBuffer.Length > 0)) Then
                            Dim str2 As String = retBuffer.ToString
                            Dim num2 As Integer = str2.LastIndexOf("\"c)
                            If (num2 > 0) Then
                                OdbcConnectionProperties._sqlNativeClientDrivers.Add(str2.Substring((num2 + 1)).ToUpperInvariant)
                            End If
                        End If
                    End If
                Next
                OdbcConnectionProperties._sqlNativeClientDrivers.Sort()
            End If
            Return OdbcConnectionProperties._sqlNativeClientDrivers
        End Get
    End Property


    ' Fields
    Private Shared _sqlNativeClientDrivers As List(Of String)
End Class


